//
//  GMAudioCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/11/6.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMAudioCell.h"
@interface GMAudioCell()
@property (weak, nonatomic) IBOutlet UIView *bagView;
@property (nonatomic, strong) AVAudioPlayer *player;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int playTimes;
@end
@implementation GMAudioCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.bagView.clipsToBounds =YES;
    self.bagView.layer.cornerRadius = 5.0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playAudio)];
    [self.bagView addGestureRecognizer:tap];
    NSMutableArray *PicArray = [NSMutableArray new];
    for (int nums = 1; nums < 3; nums++) {//三张图片
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_voice0%d.png", nums]];
        
        [PicArray addObject:image];
    }
    _img.animationImages = PicArray;
    _img.animationDuration = 1;//一次完整动画的时长
    _img.userInteractionEnabled = YES;
     self.playTimes = 0;
}
-(void)playAudio{
    [self.img startAnimating];
    //播放语音
    NSError *error;
    self.player = [[AVAudioPlayer alloc]initWithData:self.audioData error:&error];
    self.player.volume = 1.0f;
    [self.player play];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playTimeTick) userInfo:nil repeats:YES];
}
- (void)playTimeTick
{
    //当播放时长等于音频时长时，停止跳动。
    if (_playTimes >self.timeLenth ) {
        [self.img stopAnimating];
        _playTimes = 0;
        [_timer invalidate];
        return;
    }
    _playTimes += 1;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
